package com.vic.sb36.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vic.sb36.entity.Order;
import com.vic.sb36.mapper.OrderMapper;
import com.vic.sb36.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author victor
 * @since 2022-07-05
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addOrder(Order order) {
        // 为2时，抛出异常
        if(order.getQuantity() == 2) {
            throw new RuntimeException("异常啦");
        }

        this.save(order);
    }


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void addDetailWithNewTx(Order order) {
        // 为2时，抛出异常
        if(order.getQuantity() == 2) {
            throw new RuntimeException("异常啦");
        }

        this.save(order);
    }

}
